import {PermissionsAndroid} from "react-native"
import {init, Geolocation} from "react-native-amap-geolocation"
import axios, {AxiosResponse} from "axios"

/* 地理位置 当前城市信息 */
export interface ICityPosition {
  status: string
  regeocode: IRegeocode
  info: string
  infocode: string
}

/**
 * 地理位置 当前城市信息
 */
export interface IRegeocode {
  addressComponent: IAddressComponent
  formatted_address: string
}

/**
 * 地址组件
 */
export interface IAddressComponent {
  city: string
  province: string
  adcode: string
  district: string
  towncode: string
  streetNumber: IStreetNumber
  country: string
  township: string
  businessAreas: IBusinessArea[]
  building: IBuilding
  neighborhood: IBuilding
  citycode: string
  formatted_address: string
}

/**
 * 建筑信息
 */
export interface IBuilding {
  name: any[]
  type: any[]
}

/**
 * 商圈信息
 */
export interface IBusinessArea {
  location: string
  name: string
  id: string
}

/**
 * 街道信息
 */
export interface IStreetNumber {
  number: string
  location: string
  direction: string
  distance: string
  street: string
}

// 定义坐标接口
export interface Coords {
  longitude: number
  latitude: number
  accuracy: number
  altitude: number
  heading: number
  speed: number
}

class LocationManager {
  async initGeo() {
    await PermissionsAndroid.requestMultiple([
      PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
      PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION,
    ])
    await init({
      // 来自于 高德地图中的第二个应用 android 应用key
      ios: "6e8dd43a11adff29c2e56cecd7c9b3a9",
      android: "6e8dd43a11adff29c2e56cecd7c9b3a9",
    })
    return Promise.resolve()
  }

  async getCurrentPosition(): Promise<Coords> {
    return new Promise((resolve, reject) => {
      console.log("getCurrentPosition 获取当前位置")
      resolve({latitude: 23.13152, longitude: 113.42749} as Coords)
      // Geolocation.getCurrentPosition(({coords}) => {
      //   resolve({latitude: 23.13152, longitude: 113.42749} as Coords)
      //   console.log("getCurrentPosition 获取当前位置成功", coords)
      // }, reject)
    })
  }

  async getCityByLocation() {
    // const {longitude, latitude} = await this.getCurrentPosition()
    // const longitude = "113.42749"
    // const latitude = "23.13152"
    // 116.344003,40.060584
    return "越秀区新院街道35号"
    // const res = await axios.get<null, AxiosResponse<ICityPosition>>(
    //   "https://restapi.amap.com/v3/geocode/regeo",
    //   {
    //     params: {
    //       location: `${longitude},${latitude}`,
    //       key: "29586f563bf649d6204228c090f9b778",
    //     },
    //   },
    // )
    // return res.data
  }
}
export const locationManager = new LocationManager()
